package com.bwda.dsrs.filecenter.service.impl;

import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.api.OrgServiceApi;
import com.bwda.dsrs.filecenter.api.clientappversion.datadict.DatadictQueryForm;
import com.bwda.dsrs.filecenter.api.clientappversion.datadict.DatadictVo;
import com.bwda.dsrs.filecenter.api.clientappversion.group.GroupQueryForm;
import com.bwda.dsrs.filecenter.api.clientappversion.group.GroupVo;
import com.bwda.dsrs.filecenter.api.clientappversion.org.OrgCreateForm;
import com.bwda.dsrs.filecenter.api.clientappversion.user.UserForm;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.strategyfilelevel.StrategyFilelevelCondition;
import com.bwda.dsrs.filecenter.domain.condition.strategyopttype.StrategyOpttypeCondition;
import com.bwda.dsrs.filecenter.domain.condition.strategysituation.StrategySituationCondition;
import com.bwda.dsrs.filecenter.domain.condition.strategytarget.StrategyTargetCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.po.strategyfilelevel.StrategyFilelevelPo;
import com.bwda.dsrs.filecenter.domain.po.strategyopttype.StrategyOpttypePo;
import com.bwda.dsrs.filecenter.domain.po.strategysituation.StrategySituationPo;
import com.bwda.dsrs.filecenter.domain.po.strategytarget.StrategyTargetPo;
import com.bwda.dsrs.filecenter.domain.vo.strategyfilelevel.StrategyFilelevelVo;
import com.bwda.dsrs.filecenter.domain.vo.strategyopttype.StrategyOpttypeVo;
import com.bwda.dsrs.filecenter.domain.vo.strategysituation.StrategySituationVo;
import com.bwda.dsrs.filecenter.domain.vo.strategytarget.StrategyTargetVo;
import com.bwda.dsrs.filecenter.mapper.dsrs.StrategySituationMapper;
import com.bwda.dsrs.filecenter.service.StrategyFilelevelService;
import com.bwda.dsrs.filecenter.service.StrategyOpttypeService;
import com.bwda.dsrs.filecenter.service.StrategySituationService;
import com.bwda.dsrs.filecenter.service.StrategyTargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 策略管理--场景策略--服务层实现类
 *
 * @author zhangyinmei
 * @date 2019 /04/25
 */
@Service
public class StrategySituationServiceImpl extends BaseServiceImpl<StrategySituationPo, StrategySituationCondition, StrategySituationMapper> implements StrategySituationService {

    @Autowired
    private StrategyTargetService strategyTargetService;

    @Autowired
    private StrategyOpttypeService strategyOpttypeService;

    @Autowired
    private StrategyFilelevelService strategyFilelevelService;

    @Resource
    private OrgServiceApi orgServiceApi;

    /**
     * 文件等级
     */
    private static final String FILE_LEVEL = "fileLevel";
    /**
     * 操作类型
     */
    private static final String OPERATE_TYPE = "operateType";

    /**
     * 用户类型
     */
    private static final String USER_TYPE = "userType";


    @Override
    public Integer queryId() {
        return this.getMapper().queryId();
    }


    /**
     * 添加场景策略关联表数据
     *
     * @param
     */
    @Override
    public void addRelTableInfo(StrategySituationPo po) throws BusinessServiceException {
        //判断是否有三张关联表的信息
        //1.新增场景策略禁止对象
        List<StrategyTargetPo> strategyTargetPoList = po.getStrategyTargetPoList();
        if (strategyTargetPoList != null && !strategyTargetPoList.isEmpty()) {
            for (StrategyTargetPo strategyTargetPo : strategyTargetPoList) {
                strategyTargetPo.setStrategyId(po.getId());
                strategyTargetService.insert(strategyTargetPo);
            }
        }
        //2.新增场景策略禁止操作类型
        List<StrategyOpttypePo> strategyOpttypePoList = po.getStrategyOpttypePoList();
        if (strategyOpttypePoList != null && !strategyOpttypePoList.isEmpty()) {
            for (StrategyOpttypePo strategyOpttypePo : strategyOpttypePoList) {
                strategyOpttypePo.setStrategyId(po.getId());
                strategyOpttypeService.insert(strategyOpttypePo);
            }
        }
        //3.新增场景策略禁止文件等级
        List<StrategyFilelevelPo> strategyFilelevelPoList = po.getStrategyFilelevelPoList();
        if (strategyFilelevelPoList != null && !strategyFilelevelPoList.isEmpty()) {
            for (StrategyFilelevelPo strategyFilelevelPo : strategyFilelevelPoList) {
                strategyFilelevelPo.setStrategyId(po.getId());
                strategyFilelevelService.insert(strategyFilelevelPo);
            }
        }
    }


    /**
     * 获取含有关联表数据的场景策略集合场景策略集合
     *
     * @param voList : 场景策略集合
     * @return 含有关联表数据的场景策略集合
     */
    @Override
    public List<StrategySituationVo> queryRelTableInfo(List<StrategySituationVo> voList) {
        //查询 3 张关联表的数据 遍历返回列表
        for (StrategySituationVo strategySituationVo : voList) {
            //定义禁止对象集合
            List<StrategyTargetPo> strategyTargetPoList = new ArrayList<StrategyTargetPo>();
            //查询禁止对象列表
            StrategyTargetCondition strategyTargetCondition = new StrategyTargetCondition();
            //因为同一个策略同一种用户类型会对应多条数据，所以先根据场景策略ID查询出用户类型，再根据场景策略ID和用户类型，查询数据。
            strategyTargetCondition.setStrategyId(strategySituationVo.getId());
            List<StrategyTargetPo> stPoListByStrategyId = strategyTargetService.queryList(strategyTargetCondition);
            //获取已配置的用户类型，存放在set集合中
            Set<Integer> userTypeSet = new HashSet<Integer>();
            StringBuilder userCodeStr = new StringBuilder();
            if (stPoListByStrategyId != null && !stPoListByStrategyId.isEmpty()) {
                for (StrategyTargetPo stPo : stPoListByStrategyId) {
                    userTypeSet.add(stPo.getUserType());
                }
                //根据场景策略ID和用户类型，查询数据
                for (Integer userType : userTypeSet) {
                    //定义集合，存放用户编码
                    List<String> userCodeList = new ArrayList<String>();
                    strategyTargetCondition.setUserType(userType);
                    List<StrategyTargetPo> stPoListByIdAndType = strategyTargetService.queryList(strategyTargetCondition);
                    for (StrategyTargetPo stPo : stPoListByIdAndType) {
                        userCodeList.add(stPo.getUserCode());
                        if (userCodeStr.length() == 0) {
                            userCodeStr.append(stPo.getUserCode());
                        } else {
                            userCodeStr.append(",").append(stPo.getUserCode());
                        }
                    }
                    if ((int) userType == BusinessConstants.NumberConstant.ZERO) {
                        //查询人员信息
                        List<UserForm> userFormList = orgServiceApi.queryUserByCode(userCodeStr.toString());
                        //定义禁止对象实体，用于存放返回的实体
                        StrategyTargetPo strategyTargetPo = new StrategyTargetPo();
                        strategyTargetPo.setStrategyId(strategySituationVo.getId());
                        strategyTargetPo.setUserType(userType);
                        strategyTargetPo.setUserCodeList(userCodeList);
                        strategyTargetPo.setObjectList(userFormList);
                        strategyTargetPoList.add(strategyTargetPo);
                    } else if ((int) userType == BusinessConstants.NumberConstant.ONE) {
                        //查询组织信息
                        List<OrgCreateForm> orgCreateFormList = orgServiceApi.queryOrgById(userCodeStr.toString());
                        //定义禁止对象实体，用于存放返回的实体
                        StrategyTargetPo strategyTargetPo = new StrategyTargetPo();
                        strategyTargetPo.setStrategyId(strategySituationVo.getId());
                        strategyTargetPo.setUserType(userType);
                        strategyTargetPo.setUserCodeList(userCodeList);
                        strategyTargetPo.setObjectList(orgCreateFormList);
                        strategyTargetPoList.add(strategyTargetPo);
                    } else if ((int) userType == BusinessConstants.NumberConstant.TWO) {
                        //查询自定义组信息
                        List<Integer> userCodeListToLong = new ArrayList<Integer>();
                        for (String str : userCodeList) {
                            userCodeListToLong.add(Integer.parseInt(str));
                        }
                        GroupQueryForm groupQueryForm = new GroupQueryForm();
                        groupQueryForm.setIdList(userCodeListToLong);
                        ResponseEntity<CentreListResponse<GroupVo>> groupVoListResponse = orgServiceApi.queryGroupList(groupQueryForm);
                        //定义禁止对象实体，用于存放返回的实体
                        StrategyTargetPo strategyTargetPo = new StrategyTargetPo();
                        strategyTargetPo.setStrategyId(strategySituationVo.getId());
                        strategyTargetPo.setUserType(userType);
                        strategyTargetPo.setUserCodeList(userCodeList);
                        if (groupVoListResponse.getData() != null) {
                            strategyTargetPo.setObjectList(groupVoListResponse.getData().getDataList());
                        }
                        strategyTargetPoList.add(strategyTargetPo);
                    }
                }
            }
            List<StrategyTargetVo> strategyTargetVoList = CopyUtil.transfer(strategyTargetPoList, StrategyTargetVo.class);
            //获取字典名称
            toGetUserTypeDatadictName(strategyTargetVoList);
            strategySituationVo.setStrategyTargetVoList(strategyTargetVoList);
            //查询禁止文件等级列表
            StrategyFilelevelCondition strategyFilelevelCondition = new StrategyFilelevelCondition();
            strategyFilelevelCondition.setStrategyId(strategySituationVo.getId());
            List<StrategyFilelevelPo> strategyFilelevelPoList = strategyFilelevelService.queryList(strategyFilelevelCondition);
            List<StrategyFilelevelVo> strategyFilelevelVoList = CopyUtil.transfer(strategyFilelevelPoList, StrategyFilelevelVo.class);
            //获取字典名称
            toGetFileLevelDatadictName(strategyFilelevelVoList);
            strategySituationVo.setStrategyFilelevelVoList(strategyFilelevelVoList);
            //查询禁止操作类型列表
            StrategyOpttypeCondition strategyOpttypeCondition = new StrategyOpttypeCondition();
            strategyOpttypeCondition.setStrategyId(strategySituationVo.getId());
            List<StrategyOpttypePo> strategyOpttypePoList = strategyOpttypeService.queryList(strategyOpttypeCondition);
            List<StrategyOpttypeVo> strategyOpttypeVoList = CopyUtil.transfer(strategyOpttypePoList, StrategyOpttypeVo.class);
            //获取字典名称
            toGetOptTypeDatadictName(strategyOpttypeVoList);
            strategySituationVo.setStrategyOpttypeVoList(strategyOpttypeVoList);
        }
        return voList;
    }

    /**
     * 获取用户类型字典名称
     *
     * @param strategyTargetVoList
     * @return
     * @throws BusinessServiceException
     */
    private List<StrategyTargetVo> toGetUserTypeDatadictName(List<StrategyTargetVo> strategyTargetVoList) {
        //遍历集合，根据“数据字典类别code”和“字典值”获取字典名称
        for (StrategyTargetVo stVo : strategyTargetVoList) {
            stVo.setUserTypeName(toGetDatadictName(BusinessConstants.USER_TYPE, stVo.getUserType()));
        }
        return strategyTargetVoList;
    }

    /**
     * 获取字典名称
     *
     * @param dataDictTypeCode
     * @param dataDictValue
     * @return
     * @throws BusinessServiceException
     */
    private String toGetDatadictName(String dataDictTypeCode, Integer dataDictValue) {
        String result = "";
        //遍历集合，根据“数据字典类别code”和“字典值”获取字典名称
        DatadictQueryForm datadictQueryForm = new DatadictQueryForm();
        //设置数据字典类别code
        datadictQueryForm.setDataDictTypeCode(dataDictTypeCode);
        //设置字典值
        datadictQueryForm.setDataDictValue(dataDictValue.toString());
        //调用字典工具类
        ResponseEntity<CentreListResponse<DatadictVo>> responseEntity = orgServiceApi.queryDatadictList(datadictQueryForm);
        List<DatadictVo> datadictVoList = responseEntity.getData().getDataList();
        if (datadictVoList != null && !datadictVoList.isEmpty()) {
            result = datadictVoList.get(BusinessConstants.NumberConstant.ZERO).getDataDictName();
        }
        return result;
    }

    /**
     * 获取文件等级字典名字
     *
     * @param strategyFilelevelVoList
     * @return
     * @throws BusinessServiceException
     */
    private List<StrategyFilelevelVo> toGetFileLevelDatadictName(List<StrategyFilelevelVo> strategyFilelevelVoList) {
        //遍历集合，根据“数据字典类别code”和“字典值”获取字典名称
        for (StrategyFilelevelVo sfVo : strategyFilelevelVoList) {
            sfVo.setFileLevelName(toGetDatadictName(BusinessConstants.FILE_LEVEL, sfVo.getFileLevel()));
        }
        return strategyFilelevelVoList;
    }

    /**
     * 获取操作类型字典名字
     *
     * @param strategyOpttypeVoList
     * @return
     * @throws BusinessServiceException
     */
    private List<StrategyOpttypeVo> toGetOptTypeDatadictName(List<StrategyOpttypeVo> strategyOpttypeVoList) {

        //遍历集合，根据“数据字典类别code”和“字典值”获取字典名称
        for (StrategyOpttypeVo soVo : strategyOpttypeVoList) {
            soVo.setOptTypeName(toGetDatadictName(BusinessConstants.NO_OPERATE_TYPE, soVo.getOptType()));
        }

        return strategyOpttypeVoList;
    }


}